<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Deo
 * Date: 11/1/13
 * Time: 5:19 PM
 * To change this template use File | Settings | File Templates.
 */

class File_model extends CI_Model{

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function uploadFile($metadata)
    {
        $name = $metadata['name'];
        $owner = $metadata['ownerID'];
        $path = $metadata['path'];
        $fileType = $metadata['type'];
        $dateTime = $metadata['date'];
        $size = $metadata['size'];

        $sqlInsertFile = "
            INSERT INTO exp_fmis_file(Name, OwnerID, Path, Type, Date_uploaded, Size)
            VALUES(?, ?, ?, ?, ?, ?)
        ";

        $this->db->query($sqlInsertFile, array(
            $name,
            $owner,
            $path,
            $fileType,
            $dateTime,
            $size
        ));
    }

    public function getFileOwnerById($id)
    {
        $sql = "
        SELECT FirstName, LastName
        FROM exp_fmis_member_profile
        WHERE MemberID = ?
        ";
        $query = $this->db->query($sql, array(
            $id
        ));
        return $query->row();
    }

    public function getFiles($mode = 0, $filter)
    {
        /*
         * I made modes for the search functionality
         * mode 0 -> default mode
         * mode 1 -> add name filter
         * mode 2 -> add date filter
         * mode 3 -> add type filter
         * mode 4 -> add uploader filter
         * */
        $sql = "
        SELECT
            Name as 'Filename',
            exp_fmis_file.ID as 'FileId',
            MemberID,
            concat(FirstName, ' ', LastName) as 'Owner',
            Path,
            Description as 'Type',
            Date_uploaded as 'Date',
            Size
        FROM fmis.exp_fmis_file
        INNER JOIN fmis.exp_fmis_member_profile
        ON fmis.exp_fmis_file.OwnerID = fmis.exp_fmis_member_profile.MemberID
        INNER JOIN fmis.exp_fmis_file_type
        ON fmis.exp_fmis_file_type.ID = fmis.exp_fmis_file.Type
        WHERE isArchived <> 1
        ";

        switch($mode)
        {
            case 0:
                return $this->db->query($sql)->result_array();
            break;
            case 1:
                $sql.=" AND exp_fmis_file.Name LIKE ?";
                return $this->db->query($sql, $filter)->result_array();
            break;
            case 2:
                $sql.=" AND exp_fmis_file.Date_uploaded LIKE ?";
                return $this->db->query($sql, $filter)->result_array();
                break;
            case 3:
                $sql.=" AND Description LIKE ?";
                return $this->db->query($sql, $filter)->result_array();
                break;
            case 4:
                $sql.=" AND concat(FirstName, ' ', LastName) LIKE ?";
                return $this->db->query($sql, $filter)->result_array();
                break;
        }
    }

    public function archiveFile($fileId){
        $sql = "
            UPDATE exp_fmis_file
            SET isArchived = 1
            WHERE ID = ?
        ";

        $this->db->query($sql, array(
            $fileId
        ));
    }
}